package org.rsbot.event.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import org.rsbot.bot.Bot;
import org.rsbot.client.Client;
import org.rsbot.client.RSGroundData;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.Players;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

public class DrawBoundaries
  implements PaintListener
{
  private final Point[][] minimapPoints = new Point[105][105];
  private final Point[][] screenPoints = new Point[105][105];
  private MethodContext ctx;

  public DrawBoundaries(Bot paramBot)
  {
    this.ctx = paramBot.getMethodContext();
  }

  public void onRepaint(Graphics paramGraphics) {
    if (!this.ctx.game.isLoggedIn())
      return;
    int[][] arrayOfInt = this.ctx.client.getRSGroundDataArray()[this.ctx.client.getPlane()].getBlocks();
    int i = this.ctx.client.getBaseX();
    int j = this.ctx.client.getBaseY();
    int m;
    int n;
    Point localPoint3;
    Point localPoint4;
    for (int k = 0; k < this.screenPoints.length; k++) {
      for (m = 0; m < this.screenPoints[k].length; m++) {
        n = k + i - 1;
        int i1 = m + j - 1;
        localPoint3 = this.ctx.calc.worldToMinimap(n - 0.5D, i1 - 0.5D);
        if ((localPoint3.x == -1) || (localPoint3.y == -1)) {
          localPoint3 = null;
        }
        this.minimapPoints[k][m] = localPoint3;
        localPoint4 = this.ctx.calc.tileToScreen(new RSTile(n, i1), 0.0D, 0.0D, 0);
        if ((localPoint4.x == -1) || (localPoint4.y == -1)) {
          localPoint4 = null;
        }
        this.screenPoints[k][m] = localPoint4;
      }
    }

    paramGraphics.setColor(Color.YELLOW);
    for (k = 1; k < 104; k++) {
      for (m = 1; m < 104; m++) {
        n = arrayOfInt[k][m];
        Point localPoint2 = this.minimapPoints[k][m];
        localPoint3 = this.minimapPoints[k][(m + 1)];
        localPoint4 = this.minimapPoints[(k + 1)][m];
        Point localPoint5 = this.minimapPoints[(k + 1)][(m + 1)];
        Point localPoint6 = this.screenPoints[k][m];
        Point localPoint7 = this.screenPoints[k][(m + 1)];
        Point localPoint8 = this.screenPoints[(k + 1)][m];
        Point localPoint9 = this.screenPoints[(k + 1)][(m + 1)];
        if ((n & 0x1280100) != 0) {
          paramGraphics.setColor(Color.black);
          if ((localPoint8 != null) && (localPoint7 != null) && (localPoint9 != null) && (localPoint6 != null)) {
            paramGraphics.fillPolygon(new int[] { localPoint6.x, localPoint7.x, localPoint9.x, localPoint8.x }, new int[] { localPoint6.y, localPoint7.y, localPoint9.y, localPoint8.y }, 4);
          }
          if ((localPoint2 != null) && (localPoint3 != null) && (localPoint5 != null) && (localPoint4 != null)) {
            paramGraphics.fillPolygon(new int[] { localPoint2.x, localPoint3.x, localPoint5.x, localPoint4.x }, new int[] { localPoint2.y, localPoint3.y, localPoint5.y, localPoint4.y }, 4);
          }
        }
        if (((arrayOfInt[k][(m - 1)] & 0x1280102) != 0) || ((n & 0x1280120) != 0)) {
          paramGraphics.setColor(Color.RED);
          if ((localPoint8 != null) && (localPoint6 != null)) {
            paramGraphics.drawLine(localPoint6.x, localPoint6.y, localPoint8.x, localPoint8.y);
          }
          if ((localPoint2 != null) && (localPoint4 != null)) {
            paramGraphics.drawLine(localPoint2.x, localPoint2.y, localPoint4.x, localPoint4.y);
          }
        }
        if (((arrayOfInt[(k - 1)][m] & 0x1280108) != 0) || ((n & 0x1280180) != 0)) {
          paramGraphics.setColor(Color.RED);
          if ((localPoint7 != null) && (localPoint6 != null)) {
            paramGraphics.drawLine(localPoint6.x, localPoint6.y, localPoint7.x, localPoint7.y);
          }
          if ((localPoint3 != null) && (localPoint2 != null)) {
            paramGraphics.drawLine(localPoint2.x, localPoint2.y, localPoint3.x, localPoint3.y);
          }

        }

      }

    }

    Point localPoint1 = this.ctx.players.getMyPlayer().getMinimapLocation();
    paramGraphics.setColor(Color.red);
    paramGraphics.fillRect((int)localPoint1.getX() - 1, (int)localPoint1.getY() - 1, 2, 2);
  }
}